<template>
  <div id="Counterparty">
    <div class="query">
      <!-- --------------- -->
      <div class="homeTitle admin">
        <span>交易对手库</span>
        <el-input placeholder="搜索交易机构全称" v-model="search" class="input-with-select">
          <el-button slot="append" icon="el-icon-search" @click="getdata(1)"></el-button>
        </el-input>
      </div>
      <div class="queryCon">
        <div class="qSeles">
          <!-- 两个选择 -->
          <div class="xuanze">
            <div class="xuanzeCon">
              <span>交易对手所在省份：</span>
              <el-select v-model="provice" placeholder="请选择" size="small" @change="changeSheng">
                <el-option
                  v-for="item in shengOption"
                  :key="item"
                  :label="item"
                  :value="item"
                ></el-option>
              </el-select>
            </div>
            <div class="xuanzeCon">
              <span>交易对手所在地市：</span>
              <el-select v-model="city" placeholder="请选择" size="small">
                <el-option
                  v-for="item in shiOption"
                  :key="item"
                  :label="item"
                  :value="item"
                ></el-option>
              </el-select>
            </div>
            <div class="xuanzeCon">
              <span>交易对手机构类型：</span>
              <el-select v-model="type" placeholder="请选择" size="small">
                <el-option
                  v-for="item in jglx"
                  :key="item"
                  :label="item"
                  :value="item"
                ></el-option>
              </el-select>
            </div>
            <div class="xuanzeCon">
              <span>当前状态：</span>
              <el-select v-model="state" placeholder="请选择" size="small">
                <el-option
                  v-for="item in dqzt"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                ></el-option>
              </el-select>
            </div>
          </div>
          <!-- 按钮 -->
          <div class="btnAb">
            <el-button type="primary" size="small" @click="getdata(1)">查询</el-button>
            <el-button type="primary" size="small" @click="xinzeng">新增交易对手</el-button>
            <!-- <el-button type="primary" size="small" @click="$router.push('Listtransactions')">交易列表</el-button> -->
          </div>
        </div>
      </div>
    </div>
    <!-- 表格 -->
    <div class="table">
      <!-- 表格 -->
      <div class="tablecon">
        <el-table :data="tableData" border style="width: 100%">
          <el-table-column prop="identification" label="机构标识" align="center"></el-table-column>
          <el-table-column prop="type" label="交易机构类型" align="center"></el-table-column>
          <el-table-column prop="simple" label="交易机构简称" align="center"></el-table-column>
          <el-table-column prop="full" label="交易机构全称" align="center"></el-table-column>
          <el-table-column prop="phone" label="联系方式" align="center"></el-table-column>
          <el-table-column prop="creditLine" label="统一授信额度(万元)" align="center"></el-table-column>
          <el-table-column prop="available" label="当前可用额度(万元)" align="center"></el-table-column>
          <el-table-column prop="state" label="当前状态" align="center" :formatter="filterstate"></el-table-column>
          <el-table-column prop="address" label="操作" align="center" width="250">
            <template slot-scope="scope">
              <el-button size="mini" @click="lookDetail(scope.row)">维护资料</el-button>
              <el-button size="mini"  v-if="scope.row.state == 1" @click="yiru(scope.row)">移入黑名单</el-button>
              <el-button size="mini"  v-else @click="yichu(scope.row)">移出黑名单</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <!-- 分页 -->
      <div class="pgfy">
        <el-pagination
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-size="10"
          layout="total, prev, pager, next, jumper"
          :total="total"
        ></el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
import cityData from '../../../assets/js/city.js';
export default {
  name: "Counterparty",
  data() {
    return {
      dqzt: [{label: '全部', value:''},{label: '正常', value:'1'},{label: '黑名单', value:'2'}],
      jglx: ['全部','国有控股商业银行','全国性股份制商业银行','政策性银行','城市商业银行','村镇银行','省外系统内-省级银行业存款类金融机构','省外系统内-地市级银行业存款类金融机构','省外系统内-县级银行业存款类金融机构','省外系统内-其他存款类金融机构','其他银行业存款类金融机构','银行业非存款类金融机构','证券业金融机构','保险业金融机构','交易及结算类金融机构','特殊目的载体金融机构','小额贷款公司','金融控股公司','境内其他金融机构','境外同业'],
      search: "",
      tableData: [],
      // 分页
      provice: '',
      city: '',
      type: '',
      state: null,
      currentPage: 1,
      total: null,
      shengOption: [],
      shiOption: [],
      addressDetail: '',
      obj: '',
    };
  },
  mounted () {
    this.getsheng(); 
    // this.getdata(1);
  },
  methods: {
    yiru (row) {
      this.$confirm('确定要移入黑名单吗?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.axios.post('counterparty/updateState',{id: row.id, state: '2'})
          .then(({data})=> {
            if (data.code == 200) {
              this.$message({message: '移入黑名单成功',type: 'success'});
              row.state = '2'
            }
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消'
          });          
        });
    },
    yichu (row) {
       this.$confirm('确定要移出黑名单吗?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.axios.post('counterparty/updateState',{id: row.id, state: '1'})
          .then(({data})=> {
            if (data.code == 200) {
              this.$message({message: '移出黑名单成功',type: 'success'});
              row.state = '1'
            }
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消'
          });          
        });
    },
    filterstate(val, column) {
      return val[column.property]== 1 ? '正常':'黑名单';
    },
    changeSheng (val) {
      if(!val) {
        this.city = '';
      }
      let data = cityData[86];
      let shengKey = '';
      for(let k in data) {
        if(data[k]==this.provice) {
          shengKey = k;
        }
      }
      let shidata = cityData[shengKey];
      let shiOption = [];
      for(let k in shidata) {
        shiOption.push(shidata[k]);
      }
      this.shiOption = shiOption;
    },
    getsheng () {
      let data = cityData[86];
      let shengOption = [];
      for(let k in data) {
        shengOption.push(data[k]);
      }
      shengOption.unshift('全部');
      this.shengOption = shengOption;
    },
    getdata(val) {
      this.currentPage = val;
      let obj= {
        full: this.search,
        provice: this.provice,
        city: this.city,
        type: this.type,
        state: this.state,
        page: this.currentPage
      }
      this.obj = obj;
      this.axios.get('counterparty/selectList',{params:this.filterNoneObj(obj)})
        .then(({data})=> {
           if (data.code == 200) {
              this.tableData = data.data;
              this.total = data.pageInfo.total;
            }
        })
    },
    paramsGetdata(val) {
      let obj =  JSON.parse(sessionStorage.getItem('CounterpartyParams'));
      this.obj = obj;
      this.search = obj.full;
      this.provice = obj.provice;
      this.city = obj.city;
      this.type = obj.type;
      this.state = obj.state;
      this.currentPage = obj.page;
      this.axios.get('counterparty/selectList',{params:this.filterNoneObj(obj)})
        .then(({data})=> {
          if (data.code == 200) {
            this.tableData = data.data;
            this.total = data.pageInfo.total;
            this.changeSheng(true);
          }
        })
    },
    // 分页
    handleCurrentChange(val) {
      this.currentPage = val;
      this.getdata(val);
    },
    lookDetail(row) {
      sessionStorage.setItem('CounterpartyParams', JSON.stringify(this.obj));
      this.$router.push({path:'MaintainOpp', query:{id: row.id}});
    },
    xinzeng() {
      this.$router.push("OpponentDetails");
    },
  },
  beforeRouteEnter(to, from, next){
    next(vm=>{
      if(from.path=='/MaintainOpp' && sessionStorage.CounterpartyParams) {
        vm.paramsGetdata();
      } else {
        vm.getdata(1);
      }
    })
  }
};
</script>

<style lang="scss">
#Counterparty {
  .homeTitle {
    &.admin {
      display: flex;
      justify-content: space-between;
      align-items: center;
      padding: 0 20px;
      border-bottom: 1px solid black;
      .el-input {
        width: 300px;
      }
    }
  }
  .queryCon {
    background: white;
    padding: 0 20px 20px;
    .qSeles {
      padding-top: 20px;
      .xuanze {
        display: flex;
        align-items: center;
        margin-bottom: 10px;
        flex-wrap: wrap;
        .xuanzeCon {
          height: 40px;
          display: flex;
          align-items: center;
          margin-right: 30px;
          .el-input {
            width: 200px;
          }
        }
      }
      .btnAb {
        margin-top: 30px;
        .el-button {
          margin-right: 30px;
        }
      }
    }
  }
  .table {
    background: white;
    .tablecon {
      padding: 20px;
    }
    .pgfy {
      padding: 20px;
      padding-top: 10px;
      text-align: right;
    }
  }
}
</style>

